<?php



/**

 * 分类控制器

 */

namespace Custom\Controller;

use Think\Controller;

class GoodscateController extends UserController {



    function __construct()

    {

        parent::__construct();

        $this->M_cate = M('Goods_cate');

        $this->s_id = session('s_id');

    }

    /**

     *  分类列表

     */

    public function index(){

        if($this->s_id){

            $where['s_id'] = $this->s_id;

            $where['p_id'] = 0;

            $cate_list = $this->M_cate->where($where)->order('sort desc')->select();

            $this->assign('list',$cate_list);

        }

        $this->display();

    }

    /**

     *  添加修改分类

     */

    public function edit(){

        if(!$this->s_id){

            die($this->error('请先选择门店！',U('goods/index')));

        }else{

            $id = (int)$_GET['id'];

            $info = $this->M_cate->find($id);

            if(IS_POST){

                $this->M_cate->goods_cate_name = $_POST['goods_cate_name'];

                $this->M_cate->sort = (int)$_POST['sort'];

                if($id)$result = $this->M_cate->where(array('goods_cate_id'=>$id))->save();

                else {

                    $this->M_cate->s_id = (int)$this->s_id;

                    $this->M_cate->c_id = (int)$this->c_id;

                    $result = $this->M_cate->add();

                }

                if($result)$this->success(L('adsuccess'),U('Goodscate/index'));

                else $this->error(L('failed'));

            }else{

                $where['s_id'] = (int)$this->s_id;

                if($id){

                    $info = $this->M_cate->find($id);

                    $where['goods_cate_id'] = array('neq',$info['goods_cate_id']);

                    $cate_list = $this->M_cate->where($where)->select();

                }else{

                    $cate_list = $this->M_cate->where($where)->select();

                    $info['sort'] = 0;

                }

                $this->assign('info',$info);

                $this->assign('list',$cate_list);

            }

        }

        $this->display();

    }



    /**

     *  删除分类

     */

    public function delete(){

        $id = (int)$_GET['id'];

        $res = $this->M_cate->delete($id);

        if($res){

            $this->ajaxReturn(array('result'=>'tp1'));

        }else{

            $this->ajaxReturn(array('result'=>'break'));

        }

    }

    /**

     *  状态修改

     */

    public function is_show(){

        $id = $where['goods_cate_id'] = (int)$_GET['id'];

        $where['s_id'] = $this->s_id;



        $info = $this->M_cate->find($id);

        $is_show = $info['is_show']?0:1;

        $this->M_cate->is_show = $is_show;

        $this->M_cate->where($where)->save();

        $is_show?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));

    }

}